package com.polaris.live.widget

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import androidx.constraintlayout.widget.ConstraintLayout
import com.blankj.utilcode.util.StringUtils
import com.polaris.live.R.drawable
import com.polaris.live.common.ext.formatNumberWithK
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.databinding.WidgetWeekStarBinding
import com.polaris.live.resp.back_resp.ActSelfRankBaseVo
import com.polaris.live.translation.R.string.week_gift
import com.polaris.live.translation.R.string.week_rank
import com.polaris.live.translation.R.string.week_rank_delta

/**
 * 周星活动UI
 * @author Created by 天晴 on 2024/5/7 10:31
 * @since 1.0.0
 **/
class WeekStarView(context: Context, attributeSet: AttributeSet) :
    ConstraintLayout(context, attributeSet) {

    private val mBinding = WidgetWeekStarBinding.inflate(LayoutInflater.from(context), this)

    private var isShow = false

    private var back: ((show: Boolean) -> Unit)? = null
    private var detailBack: (() -> Unit)? = null

    private var lastData: ActSelfRankBaseVo? = null

    init {
        mBinding.apply {
            weekLl.setOnSingleClickListener {
                if (!isShow) {
                    weekGift.visibility = View.VISIBLE
                    weekDetail.visibility = View.VISIBLE
                    weekRank.visibility = View.VISIBLE
                    if (lastData != null && lastData?.rank == 1L) {
                        weekRankDelta.visibility = View.GONE
                    } else {
                        weekRankDelta.visibility = View.VISIBLE
                    }
                    tipImg.setImageResource(drawable.week_star_down)
                    isShow = true
                    back?.invoke(true)
                } else {
                    weekGift.visibility = View.GONE
                    weekDetail.visibility = View.GONE
                    weekRank.visibility = View.GONE
                    weekRankDelta.visibility = View.GONE
                    tipImg.setImageResource(drawable.week_star_top)
                    isShow = false
                    back?.invoke(false)
                }
            }

            weekDetail.setOnSingleClickListener {
                detailBack?.invoke()
            }
        }
    }

    private fun showData(rank: ActSelfRankBaseVo) {
        lastData = rank
        mBinding.apply {
            if (isShow) {
                if (rank.rank == 1L) {
                    weekRankDelta.visibility = View.GONE
                } else {
                    weekRankDelta.visibility = View.VISIBLE
                }
            }
            weekGift.text = StringUtils.getString(week_gift, rank.score.formatNumberWithK())
            weekRank.text = StringUtils.getString(week_rank, rank.rank.formatNumberWithK())
            weekRankDelta.text =
                StringUtils.getString(week_rank_delta, rank.rankDelta.formatNumberWithK())
        }

    }

    fun setData(rank: ActSelfRankBaseVo) {
        showData(rank)
    }

    fun setShowWeekStarCallBack(back: (show: Boolean) -> Unit) {
        this.back = back
    }

    fun setShowDetail(back: () -> Unit) {
        this.detailBack = back
    }
}